//
//  FPMainTabBarController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/1/15.
//

#import "FPMainTabBarController.h"
#import "FPMeViewController.h"
#import "FPHomePageController.h"

@interface FPMainTabBarController ()

@property (nonatomic , strong) NSString *sourcetype;

@end

@implementation FPMainTabBarController

- (instancetype)initWithContext:(nullable NSString *)context sourcetype:(NSString *)sourcetype {
    
    UIEdgeInsets imageInsets = UIEdgeInsetsZero;
    UIOffset titlePositionAdjustment = UIOffsetMake(0, -3.5);
    
    self.sourcetype = sourcetype;
    
    if (self = [super initWithViewControllers:[self viewControllersForTabBar]
                        tabBarItemsAttributes:[self tabBarItemsAttributesForTabBar]
                                  imageInsets:imageInsets
                      titlePositionAdjustment:titlePositionAdjustment
                                      context:context]) {
        
        [self customizeTabBarAppearance];
        
        self.delegate = self;
        self.navigationController.navigationBar.hidden = YES;
        
        self.tabBarController.tabBar.translucent = NO;
    }
    return self;
}

- (NSArray *)viewControllersForTabBar {
    
    FPHomePageController *firstViewController = [[FPHomePageController alloc] init];
    FPMeViewController *thirdViewController = [[FPMeViewController alloc] init];
    
    UIViewController *firstNavigationController = [[CYLBaseNavigationController alloc] initWithRootViewController:firstViewController];
    [firstViewController cyl_setHideNavigationBarSeparator:YES];
    
    UIViewController *thirdNavigationController = [[CYLBaseNavigationController alloc] initWithRootViewController:thirdViewController];
    [firstViewController cyl_setHideNavigationBarSeparator:YES];
    
    NSArray *viewControllers = @[firstNavigationController , thirdNavigationController];
    
    return viewControllers;
}

- (NSArray *)tabBarItemsAttributesForTabBar {
    
    NSDictionary *firstTabBarItemsAttributes = @{
                                                 CYLTabBarItemTitle : @"首页",
                                                 CYLTabBarItemImage : @"Home_Unselect",
                                                 CYLTabBarItemSelectedImage : @"Home_Select",
                                                 };
    
    NSDictionary *thirdTabBarItemsAttributes = @{
                                                 CYLTabBarItemTitle : @"我的",
                                                 CYLTabBarItemImage : @"Me_Unselect",
                                                 CYLTabBarItemSelectedImage : @"Me_Select",
                                                 };
    
    NSArray *tabBarItemsAttributes = @[firstTabBarItemsAttributes , thirdTabBarItemsAttributes];
    
    return tabBarItemsAttributes;
}

- (void)customizeTabBarAppearance {
    
    [self rootWindow].backgroundColor = UIColor.whiteColor; //[UIColor cyl_systemBackgroundColor];
    
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    
    normalAttrs[NSForegroundColorAttributeName] = [UIColor hexColor:@"#6A6A6A"];
    normalAttrs[NSFontAttributeName] = FONT(11);

    selectedAttrs[NSForegroundColorAttributeName] = [UIColor hexColor:@"#3A3A3A"];
    selectedAttrs[NSFontAttributeName] = FONT(11);

    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    [UITabBar appearance].translucent = NO;
    
    
//    UIImageView *v = [[UIImageView alloc] initWithFrame:CGRectMake(0, -20.f, kScreenWidth, IPHONEX_TabbarHeight+20.f)];
//    v.backgroundColor = UIColor.blueColor;
//    UIView *t = [[UIView alloc] initWithFrame:CGRectMake(0, -20.f, kScreenWidth, 20.f)];
//    [v addSubview:t];
//    t.backgroundColor = UIColor.redColor;
//
//    self.tabBar.backgroundImage = [self imageWithFrame:self.tabBar.bounds alphe:1.f];
    
    // 方式三：适配iOS13
//    if (@available(iOS 13.0, *)) {
//        // iOS13 及以上
//        self.tabBar.tintColor = UIColor.blueColor;
//        self.tabBar.unselectedItemTintColor = UIColor.redColor;
//    }
//    else {
//        // iOS13 以下
//        UITabBarItem *item = [UITabBarItem appearance];
//        [item setTitleTextAttributes:@{ NSForegroundColorAttributeName:UIColor.redColor} forState:UIControlStateNormal];
//        [item setTitleTextAttributes:@{ NSForegroundColorAttributeName:UIColor.blueColor} forState:UIControlStateSelected];
//    }
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginInvalid) name:UserTokenInvalidNotification object:nil];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    // 确保TabBar可见
    if (self.tabBar) {
        self.tabBar.hidden = NO;
        self.tabBar.alpha = 1.0;
    }
}

- (void)loginInvalid {
    
    [[FPUserModel sharedUser] loginInvalid];
}

@end
